<template>
  <div class="layout-nav">
    <div class="layout-nav__content menu">
      <div class="left">
        <div style="height:78px; line-height:78px;  display: inline-block; " >
          <img class="logo" src="//i.loli.net/2019/11/11/7PvWVAXMm9FGk4K.jpg" alt="聚好换" >
          &nbsp; &nbsp;&nbsp;
          <div style="height:78px; line-height:78px;  display: inline-block; ">
          <h style="float:right">好货集</h>
          </div>
        </div>
        <div class="cat-menu">
          <el-menu
            mode="horizontal"
            background-color="#409eef"
            text-color="#fff"
            active-text-color="#fd8">
            <el-submenu index="all" popper-class="cat-submenu">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <el-button type="text">全部商品</el-button>
              </template>
              <template  v-for="item of cats">
                <el-menu-item v-if="!item.items || item.items.length === 0" :key="item.id" :index="item.id.toString()" @click="onCatItem(item)">
                  <el-image class="cat-icon" v-if="item.icon" :src="item.icon"></el-image>
                  {{ item.name }}
                </el-menu-item>
                <el-submenu v-else :key="item.id" :index="item.id.toString()" popper-class="cat-submenu">
                  <template slot="title">
                    <el-image class="cat-icon" v-if="item.icon" :src="item.icon"></el-image>
                    <el-button type="text" @click="onCatItem(item)">{{ item.name }}</el-button>
                  </template>
                  <el-menu-item v-for="i of item.items" :key="i.id" :index="i.id.toString()" @click="onCatItem(i)">
                    <el-image class="cat-icon" v-if="i.icon" :src="i.icon"></el-image>
                    {{ i.name }}
                  </el-menu-item>
                </el-submenu>
              </template>
            </el-submenu>
          </el-menu>
        </div>
      </div>
      <div class="middle">
        <div class="searchBox">
          <el-input placeholder="请输入商品名称" ref="serchall">
          <el-button slot="append" @click="searchList();">搜索</el-button>
          </el-input>
        </div>
        <el-menu text-color="#000" active-text-color="#409eef" mode="horizontal" :router="true" :default-active="currentRoute">
          <el-menu-item index="/home">首页</el-menu-item>
          <el-menu-item index="/exchange">畅换列表</el-menu-item>
          <el-menu-item index="/hotlists">好物推荐</el-menu-item>
          <el-menu-item index="/account">账号管理</el-menu-item>
          <el-submenu index="about">
            <template slot="title">关于我们</template>
            <!-- <el-menu-item index="/aboutus">关于我们</el-menu-item> -->
            <el-menu-item index="/introduce">公司介绍</el-menu-item>
            <el-menu-item index="/media">媒体资讯</el-menu-item>
            <el-menu-item index="/contact">联系我们</el-menu-item>
          </el-submenu>
        </el-menu>
      </div>
    </div>
  </div>
</template>
<script>
  import { doCateList } from '@/apis/product';
  import { searchListParams } from '@/apis/hotlists';
  export default {
    name: 'NavComponent',
    data() {
      return {
        currentRoute: '/home',
        cats: [],
        newProductList: []
      };
    },

    methods: {
      getAllList() {
        //
      },

      async  onCatItem(item) {
        if ((item == null) || (!Object.prototype.hasOwnProperty.call(item, 'name'))) {
          alert('没有查找的名字！item.name ');
          return;
        }
        const list = await searchListParams({ productCategoryName: item.name });
        if ((list == null)) return;
        this.$emit('updateNewproduct', list);
      },

      searchList() {

      },

      async onFetchCats() {
        const res = await doCateList();
        this.cats = res.data;
      },

      onSearch() {
      // socket.send({
      //   msg: '王者荣耀',
      //   to: '13682827471'
      // });
      }
    },
    mounted() {
      const path = this.$route.path;
      if (path === '/') this.currentRoute = '/home';
      else {
        const pos = path.indexOf('/', 1);
        if (pos === -1) this.currentRoute = path;
        else this.currentRoute = path.substring(0, pos);
      }
      this.onFetchCats();
    }
  };
</script>
<style lang="scss" type="text/scss">
.searchBox {
  width: 80%;
  line-height: 78px;
  .el-input {
    input {
      border: 2px solid $c1;
    }
    div {
      margin: 0;
      color: white;
      font-size: 18px;
      background-color: $c1;
      border: 2px solid $c1;
    }
  }
}
.menu {
  .el-submenu__title, .el-menu-item {
    height: 40px !important;
    line-height: 40px !important;
    font-weight: bold;
  }
}
.cat-menu {
  ul {
    border: 0 !important;
  }
  .el-submenu__title {
    border: 0 !important;
    i {
      color: white !important;
    }
    .el-button {
      color: white !important;
      font-weight: bold;
    }
  }
}
.cat-icon {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.cat-submenu {
  .el-submenu__title {
    i, .el-button {
      color: white !important;
    }
  }
}
</style>
<style lang="scss" type="text/scss" scoped>
@import "./Nav.scss";
</style>
